*****************************************************************
*This file builds vars on schooling outcomes from the Census/ACS*
*****************************************************************

use "$list/czones_list.dta", clear
expand 3
gen year=1990
bys czone: replace year=2000 if _n==2
bys czone: replace year=2008 if _n==3

merge m:1 czone using "$clean_data_lmarket/czone1990_school_demographics.dta", assert(3) nogenerate
merge m:1 czone using "$clean_data_lmarket/czone2000_school_demographics.dta", assert(3) nogenerate
merge m:1 czone using "$clean_data_lmarket/czone2008_school_demographics.dta", assert(3) nogenerate
merge m:1 czone using "$clean_data_lmarket/czone2016_school_demographics.dta", assert(3) nogenerate

merge m:1 czone using "$clean_data_lmarket/czone1980_school_demographics.dta", assert(3) nogenerate
merge m:1 czone using "$clean_data_lmarket/czone1970_school_demographics.dta", assert(3) nogenerate


foreach var of varlist ipums_* {
quiet replace `var' = 0 if mi(`var')
}

*Schooling
foreach school in sc {

gen d_`school' = 0.7*100*((ipums_pop_`school'_2000/ipums_pop_2000)-(ipums_pop_`school'_1990/ipums_pop_1990)) if year == 1990
replace d_`school' = 100*((ipums_pop_`school'_2008/ipums_pop_2008)-(ipums_pop_`school'_2000/ipums_pop_2000)) if year == 2000
replace d_`school' = 100*((ipums_pop_`school'_2016/ipums_pop_2016)-(ipums_pop_`school'_2008/ipums_pop_2008)) if year == 2008

*Pre-trends (controls)
gen d_`school'_70_90 = 100*((ipums_pop_`school'_1990/ipums_pop_1990)-(ipums_pop_`school'_1970/ipums_pop_1970))
gen d_`school'_80_90 = 100*((ipums_pop_`school'_1990/ipums_pop_1990)-(ipums_pop_`school'_1980/ipums_pop_1980))

*Demographics: Gender and race/ethnicity
foreach dem in m f w nw {

*Using population of reference in denominator
gen d_`school'_`dem' = 0.7*100*((ipums_pop_`school'_`dem'_2000/ipums_pop_`dem'_2000)-(ipums_pop_`school'_`dem'_1990/ipums_pop_`dem'_1990)) if year == 1990
replace d_`school'_`dem' = 100*((ipums_pop_`school'_`dem'_2008/ipums_pop_`dem'_2008)-(ipums_pop_`school'_`dem'_2000/ipums_pop_`dem'_2000)) if year == 2000
replace d_`school'_`dem' = 100*((ipums_pop_`school'_`dem'_2016/ipums_pop_`dem'_2016)-(ipums_pop_`school'_`dem'_2008/ipums_pop_`dem'_2008)) if year == 2008

*Pre-trends (outcomes)
gen d_`school'_`dem'_70_80_90 = 100*((ipums_pop_`school'_`dem'_1980/ipums_pop_`dem'_1980)-(ipums_pop_`school'_`dem'_1970/ipums_pop_`dem'_1970)) if year == 1990
replace d_`school'_`dem'_70_80_90 = 100*((ipums_pop_`school'_`dem'_1990/ipums_pop_`dem'_1990)-(ipums_pop_`school'_`dem'_1980/ipums_pop_`dem'_1980)) if year == 2000

*Pre-trends (controls)
gen d_`school'_`dem'_70_90 = 100*((ipums_pop_`school'_`dem'_1990/ipums_pop_`dem'_1990)-(ipums_pop_`school'_`dem'_1970/ipums_pop_`dem'_1970))
gen d_`school'_`dem'_80_90 = 100*((ipums_pop_`school'_`dem'_1990/ipums_pop_`dem'_1990)-(ipums_pop_`school'_`dem'_1980/ipums_pop_`dem'_1980))

*Using total population in denominator
gen d_`school'_`dem'_pop = 0.7*100*((ipums_pop_`school'_`dem'_2000/ipums_pop_2000)-(ipums_pop_`school'_`dem'_1990/ipums_pop_1990)) if year == 1990
replace d_`school'_`dem'_pop = 100*((ipums_pop_`school'_`dem'_2008/ipums_pop_2008)-(ipums_pop_`school'_`dem'_2000/ipums_pop_2000)) if year == 2000
replace d_`school'_`dem'_pop = 100*((ipums_pop_`school'_`dem'_2016/ipums_pop_2016)-(ipums_pop_`school'_`dem'_2008/ipums_pop_2008)) if year == 2008

*Pre-trends (outcomes)
gen d_`school'_`dem'_pop_70_80_90 = 100*((ipums_pop_`school'_`dem'_1980/ipums_pop_1980)-(ipums_pop_`school'_`dem'_1970/ipums_pop_1970)) if year == 1990
replace d_`school'_`dem'_pop_70_80_90 = 100*((ipums_pop_`school'_`dem'_1990/ipums_pop_1990)-(ipums_pop_`school'_`dem'_1980/ipums_pop_1980)) if year == 2000

*Pre-trends (controls)
gen d_`school'_`dem'_pop_70_90 = 100*((ipums_pop_`school'_`dem'_1990/ipums_pop_1990)-(ipums_pop_`school'_`dem'_1970/ipums_pop_1970))
gen d_`school'_`dem'_pop_80_90 = 100*((ipums_pop_`school'_`dem'_1990/ipums_pop_1990)-(ipums_pop_`school'_`dem'_1980/ipums_pop_1980))

}

*Age
foreach age in a19_34 {

*Using population of reference in denominator
gen d_`school'_`age' = 0.7*100*((ipums_pop_`school'_`age'_2000/ipums_pop_`age'_2000)-(ipums_pop_`school'_`age'_1990/ipums_pop_`age'_1990)) if year == 1990
replace d_`school'_`age' = 100*((ipums_pop_`school'_`age'_2008/ipums_pop_`age'_2008)-(ipums_pop_`school'_`age'_2000/ipums_pop_`age'_2000)) if year == 2000
replace d_`school'_`age' = 100*((ipums_pop_`school'_`age'_2016/ipums_pop_`age'_2016)-(ipums_pop_`school'_`age'_2008/ipums_pop_`age'_2008)) if year == 2008

*Pre-trends (controls)
gen d_`school'_`age'_70_90 = 100*((ipums_pop_`school'_`age'_1990/ipums_pop_`age'_1990)-(ipums_pop_`school'_`age'_1970/ipums_pop_`age'_1970))
gen d_`school'_`age'_80_90 = 100*((ipums_pop_`school'_`age'_1990/ipums_pop_`age'_1990)-(ipums_pop_`school'_`age'_1980/ipums_pop_`age'_1980))

*Using total population in denominator
gen d_`school'_`age'_pop = 0.7*100*((ipums_pop_`school'_`age'_2000/ipums_pop_2000)-(ipums_pop_`school'_`age'_1990/ipums_pop_1990)) if year == 1990
replace d_`school'_`age'_pop = 100*((ipums_pop_`school'_`age'_2008/ipums_pop_2008)-(ipums_pop_`school'_`age'_2000/ipums_pop_2000)) if year == 2000
replace d_`school'_`age'_pop = 100*((ipums_pop_`school'_`age'_2016/ipums_pop_2016)-(ipums_pop_`school'_`age'_2008/ipums_pop_2008)) if year == 2008

*Pre-trends (controls)
gen d_`school'_`age'_pop_70_90 = 100*((ipums_pop_`school'_`age'_1990/ipums_pop_1990)-(ipums_pop_`school'_`age'_1970/ipums_pop_1970))
gen d_`school'_`age'_pop_80_90 = 100*((ipums_pop_`school'_`age'_1990/ipums_pop_1990)-(ipums_pop_`school'_`age'_1980/ipums_pop_1980))

}
}

*Changes in population
gen dln_pop = 0.7*100*(ln(ipums_pop_2000)-ln(ipums_pop_1990)) if year == 1990
replace dln_pop = 100*(ln(ipums_pop_2008)-ln(ipums_pop_2000)) if year == 2000
replace dln_pop = 100*(ln(ipums_pop_2016)-ln(ipums_pop_2008)) if year == 2008

*Baseline variables
foreach dem in m f w nw {
foreach year in  1970 1980 1990 2000 2008 2016 {

gen sc_`dem'_`year' = ipums_pop_sc_`dem'_`year'/ipums_pop_`dem'_`year'
gen sc_`dem'_pop_`year' = ipums_pop_sc_`dem'_`year'/ipums_pop_`year'

}

*Period specific
gen sc_`dem' = sc_`dem'_1990 if year == 1990
replace sc_`dem' = sc_`dem'_2000 if year == 2000
replace sc_`dem' = sc_`dem'_2008 if year == 2008

gen sc_`dem'_pop = sc_`dem'_pop_1990 if year == 1990
replace sc_`dem'_pop = sc_`dem'_pop_2000 if year == 2000
replace sc_`dem'_pop = sc_`dem'_pop_2008 if year == 2008

}

save "$final_data_outcomes/czone_StudentsDemographics.dta", replace






